#include "download_list.h"

#include <helpers/text/str_converter.h>
namespace DOD {

void FileDownloadList::Parse(rapidxml::xml_node<>* node)
{
	rapidxml::xml_node<>* child_node = node->first_node();
	if (NULL == child_node)
	{
		rapidxml::xml_attribute<> *attr = node->first_attribute("n");
		if (NULL != attr)
		{
			std::string n = _cur_path + node->first_attribute("n")->value();
			int32_t v = helpers::text::StringToAny<int32_t>(node->first_attribute("v")->value());
			int32_t p = 0;
			attr = node->first_attribute("p");
			if (NULL != attr)
			{
				p = helpers::text::StringToAny<int32_t>(attr->value());
			}
			_new_urls[p].push_back(UrlInfo(n, v));
		}
	}

	while (NULL != child_node) {
		_cur_path += child_node->name();
		_cur_path += '/';
		Parse(child_node);
		size_t pos = _cur_path.rfind('/', _cur_path.size()-2);
		_cur_path = _cur_path.substr(0, pos+1);
		child_node = child_node->next_sibling();
	}
}




void OldFileDownloadList::Parse(rapidxml::xml_node<>* node)
{
	rapidxml::xml_node<>* child_node = node->first_node();
	if (NULL == child_node)
	{
		rapidxml::xml_attribute<> *attr = node->first_attribute("n");
		if (NULL != attr)
		{
			std::string n = _cur_path + node->first_attribute("n")->value();
			int32_t v = helpers::text::StringToAny<int32_t>(node->first_attribute("v")->value());
			_urls.insert(std::make_pair(n, v));
		}
	}

	while (NULL != child_node) {
		_cur_path += child_node->name();
		_cur_path += '/';
		Parse(child_node);
		size_t pos = _cur_path.rfind('/', _cur_path.size()-2);
		_cur_path = _cur_path.substr(0, pos+1);
		child_node = child_node->next_sibling();
	}
}

} /// namespace DOD